<?php

class Core_Component_Catalogue_Controller_Backend_Goods{
    
    public function __construct() {
        $GLOBALS[RC]->getHelper()->setLocation('{catalogue.catalogue_menu}', '{catalogue.goods_menu}');
    }
    
    public function mappingAction(){
        return array(
            'create'=>'edit'
        );
    }    
    
    public function indexAction($pageindex = 1, $pagesize = 20){
        $q = App::getModel('catalogue/goods')->getQuery();
        $list = $q->pagination($pageindex, $pagesize)->fetch();
        $count = $q->fetchCount();
        return $GLOBALS[RC]->getHelper()->templateResult(array('list' => $list, 'record_count' => $count));
    }
    
    public function editAction($id = NULL) {
        $model = App::getModel('catalogue/goods');
        if (!is_null($id)) {
            $model->load($id);
            $GLOBALS[RC]->getHelper()->setLocation($model->getName());
        }else{
            $GLOBALS[RC]->getHelper()->setLocation('{catalogue.create_goods}');
        }
        $categories = App::getModel('catalogue/category')
                        ->getQuery()
                        ->condition('v.website_id', App::getRunningWebsite()->getId())
                        ->fetch();
        return $GLOBALS[RC]->getHelper()->modelFormResult($model,
                array('category_id'=>array('data'=>$categories->toArray(), 'labelKey'=>'name', 'valueKey'=>'id')),
                '*/*/edit');
    }
    
    public function saveAction(Core_Component_Catalogue_Model_Goods $model) {
        App::getBusiness('catalogue/goods')->saveGoods($model);
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');
    }
    
    public function deleteAction($id) {
        App::loadModel('cms/category', $id)->delete();
        $GLOBALS[RC]->getMessager()->addSuccess('{system.message.success}');
        return $GLOBALS[RC]->getHelper()->historyResult('*/*/*/index');
    }    
}